#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
"""
@File    :   pppoe.py
@Time    :   2024-05-15 10:17
@Author  :   坐公交也用券
@Version :   1.0
@Contact :   faith01238@hotmail.com
@Homepage : https://liumou.site
@Desc    :   用于Linux系统配置PPPoE拨号
"""
import logging
import subprocess
import sys
from os import system

# 创建一个自定义的日志记录器
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

# 创建一个自定义的日志处理器，设置其输出格式
formatter = logging.Formatter('%(asctime)s [%(levelname)s] %(filename)s:%(lineno)d | %(funcName)s | %(message)s')
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(formatter)
logger.addHandler(handler)


class PppoeConfig:
	def __init__(self, config: str, debug: bool = False):
		self.default_config = config
		self.debug = debug
		self.config = config
		self.txt_config = {}
		self.log_file = None
		self.current_min = 0
		self.current_hour = 0
		self.package_manager = "apt"

	def check_package(self):
		# 获取当前系统的包管理器
		if system("which apt") == 0:
			self.package_manager = "apt"
			return
		elif system("which yum") == 0:
			self.package_manager = "yum"
			return
		elif system("which dnf") == 0:
			self.package_manager = "dnf"
			return
		elif system("which zypper") == 0:
			self.package_manager = "zypper"
			return
		elif system("which pacman") == 0:
			self.package_manager = "pacman"
			return
		elif system("which apk") == 0:
			self.package_manager = "apk"
			return
		elif system("which emerge") == 0:
			self.package_manager = "emerge"
			return
		elif system("which pkg") == 0:
			self.package_manager = "pkg"
			return
		elif system("which opkg") == 0:
			self.package_manager = "opkg"
			return
		else:
			print("不支持的包管理器")
			exit(1)

	def install_pppoe(self):
		"""
		安装PPPOE
		:return:
		"""
		logger.info("正在安装PPPOE...")
		r = subprocess.getstatusoutput(f"{self.package_manager} install pppoe pppoeconf -y")
		if r[0] != 0:
			logger.error("无法安装pppoe")
			print(r[1])
			sys.exit(2)
		system("pppoeconf")

	def start(self):
		self.check_package()
		self.install_pppoe()


if __name__ == '__main__':
	p = PppoeConfig("")
	p.start()
